﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using SdlDotNet.Graphics;
using System.IO;
using System.Drawing;

namespace bloodbox
{
    class MapDrawer
    {
        
        private static int[,] TabMap = new int[20, 20];
        private static Surface tileset;
        private Surface map;
        private Surface WindowsScreen;
        private static SurfaceCollection tilesCollection = new SurfaceCollection();
        
        private static string name = "";
        private static int id;

        private int MapX;
        private int MapY;

        private static int ScreenCenterX = 512;
        private static int ScreenCenterY = 384;

        public MapDrawer(Surface Screen)
        {
            MapX = 100;
            MapY = 100;

            WindowsScreen = Screen;
        }

        /// <summary>Loads the map from the XML file</summary>
        /// <param name="name">File name of the map</param>
        public void loadXMLMap()
        {
            
            XmlTextReader reader = new XmlTextReader("maps/xml/" + id + ".tmx");

            int i = 0;
            int j = 0;
            while (reader.Read())
            {
                // http://msdn.microsoft.com/fr-fr/library/9b9dty7d(VS.80).aspx

                if (reader.NodeType == XmlNodeType.Element) // The node is an element.
                {
                    while (reader.MoveToNextAttribute())
                    { // Read the attributes.
                        if (reader.Name == "gid")
                        {
                            TabMap[i, j] = Convert.ToInt32(reader.Value);
                            if (i == 19)
                            {
                                i = 0;
                                j++;
                            }
                            else
                            {
                                i++;
                            }
                        }
                    }
                }

            }
        }
        
        /// <summary>Loads the map (generic method)</summary>
        /// <param name="map_id">id of the map</param>
        public void loadMap(int map_id)
        {
            id = map_id;

            loadXMLMap();  // Loading of the XML file
            loadTileset(); // Loading of the tileset
        }

        public void MoveMap(int X, int Y)
        {
            MapX += ScreenCenterX - X;
            MapY += ScreenCenterY - Y;
        }

        /// <summary>
        /// Loads the tileset of the map an cuts floor tiles
        /// </summary>
        public void loadTileset()
        {
            tileset = new Surface(Path.Combine("maps", Path.Combine("tilesets", id+".png")));
            
            int m = 0;

            Size size = new Size(160, 80);

            while (m < 174)
            {
                tilesCollection.Add(tileset, size, m);
                tilesCollection[m].TransparentColor = Color.Magenta;
                tilesCollection[m].Transparent = true;
                m++;
            }
        }

        public Surface drawMap()
        {
            int i = 0;
            int j = 0;

            Point point = new Point();
            
            while (j < 20)
            {
                while (i < 20)
                {
                    point.Y = MapY + (i+j)*40;
                    point.X = MapX + ((i + (20 - j)) * 80 - 39);

                    WindowsScreen.Blit(tilesCollection[TabMap[i, j] - 1], point);

                    i++;
                }
                i = 0;
                j++;
            }
            return map;
        }
    }
}
